package club.janna.study.mybatis;

import club.janna.study.mybatis.domain.User;
import club.janna.study.mybatis.repository.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MybatisDemo {
    public static void main(String[] args) throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        // 初始化session factory
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 创建session
        SqlSession sqlSession = sessionFactory.openSession();

        // 使用sqlSession方式查询用户列表
//        List<User> users = sqlSession.selectList("club.janna.study.mybatis.repository.UserMapper.list");

        // 使用Mapper方式查询用户列表
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.list();

        users.forEach(System.out::println);
    }
}
